﻿@using eBusiness.App_DAL
@model IEnumerable<eBusiness.Models.Category>

@functions {
    private string GetCategoryLink(int pageNumber = 1)
    {
        var link = "/Admin/Categories/Index?";
        
        if (!string.IsNullOrEmpty(ViewBag.CurrentSoftField))
        {
            link += "&softField=" + ViewBag.CurrentSoftField;
        }
        if (!string.IsNullOrEmpty(ViewBag.CurrentSoftDir))
        {
            link += "&softDir=" + ViewBag.CurrentSoftDir;
        }
        if (!string.IsNullOrEmpty(pageNumber.ToString()))
        {
            link += "&pageNumber=" + pageNumber;
        }
        return link;
    }
}


@{
    ViewBag.Title = "List Category";
    Layout = "../Shared/_Layout.cshtml";
}

<script type="text/javascript">
    $(document).ready(function () {
        $("#SoftField").change(function (e) {
            window.location.href = '/Admin/Categories?' + GetParams();
        });
        $("#SoftDir").change(function (e) {
            window.location.href = '/Admin/Categories?' + GetParams();
        });
    });

    function GetParams() {
        var softField = $("#SoftField :selected").val();
        var softDir = $("#SoftDir :selected").val();

        var param = '';
        if (!(softField == '' || softField == null)) {
            param += '&softField=' + softField;
        }
        if (!(softDir == '' || softDir == null)) {
            param += '&softDir=' + softDir;
        }
        return param;
    }
</script>

<h2>
    Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>

<div class="adminfilterdiv">
    <ul class="productFilter">        
        <li><span>Sort By : </span><span>
            @Html.DropDownList("SoftField", (SelectList)ViewBag.SoftFields, string.Empty)
        </span></li>
        <li><span>Sort Dir : </span><span>
            @Html.DropDownList("SoftDir", (SelectList)ViewBag.SoftDir, string.Empty)
        </span></li>
    </ul>
</div>

<table class="dashboardTable">
    <tr>
        <th>
            Icon
        </th>
        <th>
            Name
        </th>
        <th>
            Description
        </th>
        <th>
            IsMenu
        </th>
        <th>
            ParentID
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                <img src="@item.Icon" alt="icon" class="thumbImage" />
            </td>
            <td>
                <a href="@Url.Action("Details", "Categories", new { id = item.ID })">@Html.DisplayFor(modelItem => item.Name)</a>                        
            </td>
            <td>
                @item.Description.ToString().GetLeft(200, true)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IsMenu)
                @if (@item.IsMenu && (!string.IsNullOrEmpty(item.IsMenuStartDate.ToString()) || !string.IsNullOrEmpty(item.IsMenuEndDate.ToString())))
                {
                    <div>
                        <span class="leftDetailsTableColumn">From </span>
                        @if (string.IsNullOrEmpty(item.IsMenuStartDate.ToString()))
                        {
                            <span>n/a</span>
                        }
                        else
                        {
                            @item.IsMenuStartDate.Value.ToString("dd/MM/yyyy");
                        }
                        <span class="leftDetailsTableColumn">To </span>
                        @if (string.IsNullOrEmpty(item.IsMenuEndDate.ToString()))
                        {
                            <span>n/a</span>
                        }
                        else
                        {
                            @item.IsMenuEndDate.Value.ToString("dd/MM/yyyy");
                        }
                    </div>
                }
            </td>
            <td>
                @if (item.ParentID != null && item.ParentID > 0)
                {
                    <a href="@Url.Action("Details", "Categories", new { id = item.ParentID })">@Html.DisplayFor(modelItem => item.Parent.Name)</a>
                }                
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                @Html.ActionLink("Details", "Details", new { id = item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.ID })
            </td>
        </tr>
    }
</table>

<div class="common_paging">
    <ul class="nav_common_ul">
        @{
            if (ViewBag.CurrentPage > 1)
            {
                
            <li><a href="@GetCategoryLink()" class="common_paging_first">First</a></li>
            <li><a href="@GetCategoryLink((int)ViewBag.CurrentPage - 1)" class="common_paging_previous">
                Prev</a></li>
            }
            <li>page
                <input id="goToPage" type="text" class="common_pagging_page" style="width: 50px" value="@ViewBag.CurrentPage"/>
                of @ViewBag.NumberOfPage
            </li>
            if (ViewBag.CurrentPage < ViewBag.NumberOfPage)
            {
            <li><a href="@GetCategoryLink((int)ViewBag.CurrentPage + 1)" class="common_paging_next">
                Next</a></li>
            <li><a href="@GetCategoryLink((int)ViewBag.NumberOfPage)" class="common_paging_last">
                Last</a></li>
            }
        }
    </ul>
</div>